﻿using Microsoft.AspNetCore.Mvc;
using WebApplicationKG.Models.Chp01Enity;

namespace WebApplicationKG.Controllers
{
    public class Chp01Controller : Controller
    {
        #region Hello World
        public IActionResult Demo01()
        {
            return View();
        }
        #endregion

        #region ViewData的使用
        public IActionResult Demo02()
        {           
            var emp = new Employee();
            emp.EmpId = 1;
            emp.EmpName = "孙悟空";
            emp.EmpPhone = "1234567890";
            ViewData["Info"] = "这是我的第一个MVC程序";
            ViewData["Emp"] = emp;
            return View();
        }
        #endregion

        #region ViewBag的使用
        public IActionResult Demo03()
        {
            var emp = new Employee();
            emp.EmpId = 1;
            emp.EmpName = "孙悟空";
            emp.EmpPhone = "1234567890";
            ViewBag.Info= "这是我的第一个MVC程序";
            ViewBag.Emp = emp;
            return View();
        }
        #endregion

        #region 登录页面
        public IActionResult LoginForm()
        {
          
            return View();
        }
        #endregion


        #region 用于处理登录的请求
        public IActionResult Login()
        {
            //假设用户名和密码都是admin可以登录成功，否则登录失败
            string acc=Request.Form["txtAccount"];
            string pwd = Request.Form["txtPwd"];
            if (acc.Equals("admin") && pwd.Equals("admin"))
            {
                //跳转
                return RedirectToAction("WelcomForm");
            }
            else 
            {
                //提示
                TempData["ErrInfo"] = "用户名或密码错误！";

                return RedirectToAction("LoginForm");
            }

           
        }
        #endregion


        #region 登录成功之后的欢迎界面
        public IActionResult WelcomForm()
        {

            return View();
        }
        #endregion

        #region 同名Action 处理登录页面
        [HttpGet]
        public IActionResult DL()
        {

            return View();
        }
        #endregion

        #region 同名Action 处理登录成功之后的请求
        [HttpPost]
        public IActionResult DL(string acc ,string pwd)
        {

            //假设用户名和密码都是admin可以登录成功，否则登录失败            
            if (acc.Equals("admin") && pwd.Equals("admin"))
            {
                //跳转
                return RedirectToAction("WelcomForm");
            }
            else
            {
                //提示 TempData,默认保存在Session中
                TempData["ErrInfo"] = "用户名或密码错误！";

                return RedirectToAction("DL");
            }

       
        }
        #endregion
    }
}
